<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <!-- <a-col :span="24">
            <a-form-model-item label="项目id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="equipmentId">
              <j-dict-select-tag type="list" v-model="model.equipmentId" dictCode="" placeholder="请选择项目id" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="PLC参数名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterName">
              <j-popup
                v-model="model.parameterName"
                field="parameterId"
                org-fields="parameter_name,id,parameter_plc_id,parameter_class,parameter_range,parameter_control_object_name,param_address,parameter_data_type,param_ref_value,parameter_unit"
                dest-fields="parameterName,parameterId,parameterPlcId,parameterType,parameterActualRange,parameterSecondtype,paramAddress,parameterDatatype,paramRefvalue,parameterUnit"
                code="plcParamQuery"
                :param="popParam"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="PLC参数ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterPlcId">
              <a-input :disabled="true" v-model="model.parameterPlcId" placeholder="请选择PLC参数ID" ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="PLC参数名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterName">
              <a-input v-model="model.parameterName" placeholder="请输入PLC参数名称"  ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24" >
            <a-form-model-item label="PLC变量所属一级组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterType">
              <j-dict-select-tag :disabled="true" type="list" v-model="model.parameterType" dictCode="parameter_class" placeholder="请选择PLC变量所属一级组" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="PLC变量所属二级组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterSecondtype">
              <a-input :disabled="true"  v-model="model.parameterSecondtype" placeholder="请选择PLC变量所属二级组" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="PLC变量地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paramAddress">
              <a-input :disabled="true"  v-model="model.paramAddress" placeholder="请输入PLC变量地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="项目信息类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterGroup">
              <j-dict-select-tag type="list" v-model="model.parameterGroup" dictCode="parameter_group" placeholder="请选择项目信息类别" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="PLC数据类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterDatatype">
              <j-dict-select-tag :disabled="true" type="list" v-model="model.parameterDatatype" dictCode="parameter_data_type" placeholder="请选择PLC数据类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="PLC变量参考值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paramRefvalue">
              <a-input :disabled="true" v-model="model.paramRefvalue" placeholder="请输入PLC变量参考值"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="PLC变量单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterUnit">
              <a-input :disabled="true" v-model="model.parameterUnit" placeholder="请输入PLC变量单位"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="零部件位号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="locationNumber">
              <a-input v-model="model.locationNumber" placeholder="请输入位号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="配置人员角色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="configRoles">
              <a-input v-model="model.configRoles" placeholder="请输入项目PLC参数操作人员角色"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="PLC实际参数取值范围" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterActualRange">
              <a-input :disabled="true" v-model="model.parameterActualRange" placeholder="请输入PLC实际参数取值范围"  ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="是否修正变量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isRevise">
              <j-dict-select-tag type="radio" v-model="model.controlRequired" dictCode="yn" placeholder="请选择是否修正变量" />
            </a-form-model-item>
          </a-col>  -->
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <a-textarea v-model="model.remark" placeholder="请输入备注"  ></a-textarea>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="控件类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlType">
              <a-input v-model="model.controlType" placeholder="请输入控件类型"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="控件值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlValue">
              <a-input v-model="model.controlValue" placeholder="请输入控件值"  ></a-input>
            </a-form-model-item>
          </a-col>-->
         
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'YksEquipmentConfigurationPlcForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        action:'',
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        currentEquipmentId:'',
        validatorRules: {
          parameterName: [
              { required: true, message: '请输入PLC参数名称!'},
           ],
           parameterGroup: [
              { required: true, message: '请输入项目信息类别!'},
           ],
          //  parameterActualRange: [
          //     { required: true, message: '请输入PLC实际参数取值范围!'},
          //  ],
        },
        popParam:{},
        equipmentType:'',
        url: {
          add: "/dev/yksEquipmentConfigurationPlc/add",
          edit: "/dev/yksEquipmentConfigurationPlc/edit",
          queryById: "/dev/yksEquipmentConfigurationPlc/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.action = "add"
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.popParam = {equipment_type:this.equipmentType}
        this.model = Object.assign({}, record);
        if (this.action=="add") {
          this.model.equipmentId = this.currentEquipmentId
        }
        
        this.visible = true;
      },
      
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      },
    }
  }
</script>